/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.filter;

/**
 *
 * @author joao
 */
import java.io.Serializable;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class Conecta implements Serializable {

    private static final long serialVersionUID = 1L;
    private final static Logger logger = Logger.getLogger(Conecta.class.getName());
    private Statement declaracao = null;
    private PreparedStatement declPreparada = null;
    private Connection conexao = null;
    private final ResultSet resultado = null;
    private int nErro = 0;
    private final String atualiza = "";
    private String url;
    private String user;
    private String password;
    private static final String JNDINome = "jdbc/financeiroDS";

    /**
     * Creates a new instance of Conecta
     * @return 
     * @throws java.sql.SQLException
     */
    public Connection Conecta() throws SQLException {
        try {
            conexao = getConexao(JNDINome);
            conexao.setAutoCommit(false);

            //Class.forName("org.postgresql.Driver");
            //conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/salao", "salao", "salao");
            //conexao = DriverManager.getConnection("jdbc:firebirdsql:192.168.1.99:/opt/firebird/data/apolo2.fdb?lc_ctype=WIN1252","sysdba","masterkey");
            //conexao.setAutoCommit(false);
            //declaracao = conexao.createStatement();

        } catch (SQLException e) {
            //System.out.println("Nao foi possivel Conectar!");
            Logger.getLogger(Conecta.class.getName()).log(Level.SEVERE, null, e);
        } catch (Exception e) {
            //System.out.println("Driver não carregado!");
            Logger.getLogger(Conecta.class.getName()).log(Level.SEVERE, null, e);
        }
        return conexao;
    }

    public static Connection getConexao(String JNDINome) throws Exception {
        Connection con = null;

        // Obtém a raiz da hierarquia de nomes
        InitialContext contexto = new InitialContext();

        // Obtém a origem dos dados
        DataSource ds = (DataSource) contexto.lookup("java:comp/env/" + JNDINome);

        // Obtém uma conexão
        con = ds.getConnection();

        // Retorna a conexão
        return con;
    }

    public void close() throws SQLException {
        if (conexao != null) {
            conexao.close();
            conexao = null;
        }

    }

    public void fechar() {
        try {
            if (declPreparada != null) {
                declPreparada.close();
                declPreparada = null;
            }
            if (declaracao != null) {
                declaracao.close();
                declaracao = null;
            }

            if (conexao != null) {
                conexao.close();
                conexao = null;
            }
        } catch (SQLException e) {
            nErro = -1;
        }
    }
}
